package com.huawei.esdk.tp.device.aa;

import java.util.Date;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.MessageContext;
import com.huawei.esdk.platform.common.ThreadLocalHolder;
import com.huawei.esdk.platform.common.bean.log.InterfaceLogBean;
import com.huawei.esdk.platform.common.utils.ApplicationContextUtil;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.platform.commu.itf.ISDKProtocolAdapter;
import com.huawei.esdk.platform.exception.ProtocolAdapterException;
import com.huawei.esdk.platform.log.itf.IInterfaceLog;

public class TerminalAAService
{
    private static final Logger LOGGER = Logger
            .getLogger(TerminalAAService.class);

    protected ISDKProtocolAdapter protocolAdapter;

    public TerminalAAService(ISDKProtocolAdapter protocolAdapter)
    {
        this.protocolAdapter = protocolAdapter;
    }

    public String terminalLoginRequest()
    {
        LOGGER.debug("TerminalLoginRequest process start");

        String response = null;
        try
        {
            response = sendJsonMessage("", "WEB_RequestSessionIDAPI");
        }
        catch (ProtocolAdapterException e)
        {
            response = null;
            LOGGER.error("", e);
        }

        LOGGER.debug("TerminalLoginRequest process end");
        return response;
    }

    public String terminalAuthorize(String userName, String password)
            throws ProtocolAdapterException
    {
        LOGGER.debug("SMCAuthorize process start");

        String requestMessage = "{\"user\"" + ":" + "\"" + userName + "\""
                + "," + "\"password\"" + ":" + "\"" + password + "\"}";
        String logMessage = "{\"user\"" + ":" + "\"" + userName + "\"" + ","
                + "\"password\"" + ":" + "\"********\"}";
        LOGGER.debug("request Json Message = " + logMessage);
        String response = sendJsonMessage(requestMessage,
                "WEB_RequestCertificateAPI");

        LOGGER.debug("TerminalAuthorize process end");
        return response;
    }

    public String terminalLogout()
    {
        LOGGER.debug("TerminalLogout process start");
        String response = null;
        try
        {
            response = sendJsonMessage("", "WEB_LogoutAPI");
        }
        catch (ProtocolAdapterException e)
        {
            response = null;
            LOGGER.error("", e);
        }
        LOGGER.debug("TerminalLogout process end");
        return response;
    }

    public String terminalKeepAlive() throws ProtocolAdapterException
    {
        LOGGER.debug("TerminalKeepAlive process start");

        return sendJsonMessage("", "WEB_GetMailboxDataAPI");
    }

    protected String sendJsonMessage(String requestMessage, String srcMethod)
            throws ProtocolAdapterException
    {
        if (!"WEB_RequestCertificateAPI".equals(srcMethod))
        {
            LOGGER.debug("request Json Message = " + requestMessage);
        }
        String res = protocolAdapter.syncSendMessage(requestMessage, srcMethod);

        
        // interface log
        if (!StringUtils.isEmpty(res) && res.contains("success"))
        {
            int begin = res.indexOf(":", res.indexOf("success")) + 1;
            int end = res.indexOf(",", res.indexOf("success"));
            if (begin < end)
            {
                String resultCode = res.substring(begin, end);
                MessageContext mc = ThreadLocalHolder.get();
                if (null != mc)
                {
                    String uuid = (String)mc.getEntities().get("interfaceLogger-TransactionId");
                    InterfaceLogBean bean = new InterfaceLogBean();
                    bean.setTransactionId(uuid);
                    bean.setReq(false);
                    bean.setRespTime(new Date());
                    bean.setResultCode(resultCode);
                    
                    IInterfaceLog interfaceLogger = ApplicationContextUtil.getBean("interfaceLogger");
                    interfaceLogger.info(bean);
                }
            }
        }
        
        return res;
    }
}
